package top.dfghhj.anhanx.oauth.common.impl;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import top.dfghhj.anhanx.oauth.convertor.SysMenuConvertor;
import top.dfghhj.anhanx.oauth.convertor.SysRoleConvertor;
import top.dfghhj.anhanx.oauth.convertor.SysUserConvertor;
import top.dfghhj.anhanx.oauth.entity.DO.SysMenu;
import top.dfghhj.anhanx.oauth.entity.DO.SysRole;
import top.dfghhj.anhanx.oauth.entity.DO.SysUser;
import top.dfghhj.anhanx.oauth.entity.DTO.LoginUser;
import top.dfghhj.anhanx.oauth.entity.DTO.SysMenuDTO;
import top.dfghhj.anhanx.oauth.entity.DTO.SysRoleDTO;
import top.dfghhj.anhanx.oauth.entity.DTO.SysUserDTO;
import top.dfghhj.anhanx.oauth.service.SysMenuService;
import top.dfghhj.anhanx.oauth.service.SysRoleService;
import top.dfghhj.anhanx.oauth.service.SysUserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysMenuService sysMenuService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<SysUser> sysUserList = sysUserService.findByUsername(username);
        if (CollectionUtils.isEmpty(sysUserList)) {
            throw new UsernameNotFoundException("用户不存在");
        } else {
            SysUser sysUser = sysUserList.get(0);
            Set<GrantedAuthority> authorities = new HashSet<>();
            List<SysRoleDTO> roles = new ArrayList<>();
            List<SysMenuDTO> menus = new ArrayList<>();

            List<SysRole> sysRoleList = sysRoleService.findByUserId(sysUser.getId());
            if (CollectionUtils.isEmpty(sysRoleList)) {
                throw new UsernameNotFoundException("用户权限不足");
            } else {
                for (SysRole sysRole : sysRoleList) {
                    roles.add(SysRoleConvertor.toDTO(sysRole));
                    authorities.add(new SimpleGrantedAuthority("ROLE_" + sysRole.getRoleName()));
                    List<SysMenu> sysMenuList = sysMenuService.findByRoleId(sysRole.getId());
                    if (!CollectionUtils.isEmpty(sysMenuList)) {
                        for (SysMenu sysMenu : sysMenuList) {
                            menus.add(SysMenuConvertor.toDTO(sysMenu));
                            authorities.add(new SimpleGrantedAuthority(sysMenu.getPermission()));
                        }
                    }
                }
            }
            SysUserDTO sysUserDTO = SysUserConvertor.toDTO(sysUser);
            sysUserDTO.setSysRoles(roles);
            sysUserDTO.setSysMenus(menus);
            return new LoginUser(sysUserDTO, authorities);
        }
    }



}
